﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web;


/// <summary>
///MComment 的摘要说明
/// </summary>
public class MComment
{
    private static readonly string _connectionString;


    private int m_iID;

    public int IID
    {
        get { return m_iID; }
    }
    private int m_iUserID;

    public int IUserID
    {
        get { return m_iUserID; }
    }
    private DateTime m_dtDate;

    public DateTime DtDate
    {
        get { return m_dtDate; }
    }
    private int m_iMovieID;

    public int IMovieID
    {
        get { return m_iMovieID; }
    }

    private string m_sTitle;

    public string STitle
    {
        get { return m_sTitle; }
    }
    private string m_sDetail;

    public string SDetail
    {
        get { return m_sDetail; }
    }



    public MComment(int id, int userid, int movieid, DateTime date
        , string title, string detail)
    {
        m_iID = id;
        m_iUserID = userid;
        m_iMovieID = movieid;
        m_dtDate = date;
        m_sDetail = detail;
        m_sTitle = title;


        //
        //TODO: 在此处添加构造函数逻辑
        //
    }


    /// <summary>
    /// 添加一条评论到数据库		静态
    /// </summary>
    /// <param name="userid">userid</param>
    /// <param name="movieid">movieid</param>
    /// <param name="title">title</param>
    /// <param name="detail">detail</param>
    public static bool AddM_Comment(int userid, int movieid, DateTime time, string title, string detail)
    {

		int nT = 0;
		if (title != null)
			++nT;
		if (detail != null)
			++nT;
		nT += 3;
		string[] name = new string[nT];
		object[] value = new object[nT];
		name[0] = "MovieID";
		value[0] = movieid;
		name[1] = "Date";
		value[1] = time;
		name[2] = "UserID";
		value[2] = userid;
		nT = 2;
		if (title != null)
		{
			name[++nT] = "Title";
			value[nT] = title;
		}
		if (detail != null)
		{
			name[++nT] = "Detail";
			value[nT] = detail;
		}

		try
		{

			return DataBaseClass.insertData(name, value, "M_Comment", DataBaseClass.GetConnectionString());
			
		}
		catch (SqlException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (Exception e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return false;
		}
		//string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
		//string[] value = { userid.ToString(), movieid.ToString(), date, title, detail };

		//DataBaseClass.insertData(value, "M_Comment", _connectionString);
    }

    /// <summary>
    ///2)保存自身到数据库
    /// </summary>
    /// <param name="mcom">评论对象 </param>
    public bool UpdateM_Comment()
    {
		bool bT = AddM_Comment(this.IUserID, this.IMovieID, this.DtDate, this.STitle, this.SDetail);
		m_iID = DataBaseClass.GetMaxID("M_Comment" , DataBaseClass.GetConnectionString());
		return bT;
		//string date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
		//string[] value = { mcom.m_iUserID.ToString(), mcom.m_iMovieID.ToString(), date, mcom.m_sTitle, mcom.m_sDetail };

		//DataBaseClass.insertData(value, "M_Comment", _connectionString);
    }

    //3)删除评论
    /// <summary>
    /// 删除评论
    /// </summary>
    /// <param name="id">待删除的id号</param>
    /// <returns>返回是否成功</returns>
    public static bool DelM_Comment(int id)
	{
		try
		{
			bool succ = DataBaseClass.deleteData("ID", id, "M_Comment", _connectionString);
			return succ;
		}
		catch (SqlException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return false;
		}
		catch (Exception e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return false;
		}

    }



    /// <summary>
    /// 获得一个电影的评论
    /// </summary>
    /// <param name="ID">电影ID</param>
    /// <returns></returns>
    public static DataTable GetM_Comment(int ID)
    {
		try{

		return DataBaseClass.selectData("SELECT * FROM M_Comment WHERE MovieID = " + ID , _connectionString);
		}
		catch (SqlException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (ArgumentException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (InvalidOperationException e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine(e.Message);
			return null;
		}
		catch (Exception e)
		{
			System.Console.Write("MComment类：");
			System.Console.WriteLine("未知错误:" + e.Message);
			return null;
		}
        string sql;
        //switch (type)
        //{
        //    case "date":
        //        DateTime time = (DateTime)detail;
        //        DateTime now = DateTime.Now ;
        //        sql = "SELECT * FROM M_Comment WHERE Date BETWEEN "+time.ToString()+" and "+now.ToString ();
        //        break ;
        //    case "movie":
        //string movie = detail;
        sql = "SELECT * FROM M_Comment WHERE MovieID = " + ID + ";";
        //        break;
        //    case "user":
        //        string user = (string)detail;
        //        sql = "SELECT * FROM M_Comment WHERE UserID = '" + user + "';";
        //        break;
        //    default :

        //        break;
        //}

        SqlDataAdapter dad = new SqlDataAdapter(sql, _connectionString);//here!!!!!!!!!!!!!!!!!!!!!!!!
        DataTable dtblMCom = new DataTable();
        dad.Fill(dtblMCom);

        return dtblMCom;
    }

    static MComment()
    {
		_connectionString = DataBaseClass.GetConnectionString();
    }

    public static List<MComment> GetListFromDataTable(DataTable dt)
    {
        List<MComment> temp = new List<MComment>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            MComment mcomment = new MComment((int)dt.Rows[i]["ID"], (int)dt.Rows[i]["UserID"]
                , (int)dt.Rows[i]["MovieID"], (DateTime)dt.Rows[i]["Date"]
                , dt.Rows[i]["Title"] as string, dt.Rows[i]["Detail"] as string);
            temp.Add(mcomment);
        }
        return temp;
    }
	public bool DelM_Comment()
	{
		return DelM_Comment(this.IID);
	}
	
}
public class Test5
{
	public static void Test()
	{
		List<DataBaseHelper.Movie> m = DataBaseHelper.Movie.GetMovieListByDataTable(DataBaseHelper.Movie.GetMovieByName("asdfar"));
		foreach (DataBaseHelper.Movie mo in m)
			mo.DeleteItselfFromDatabase();
		DataBaseHelper.Movie movie = new DataBaseHelper.Movie(-1, "asdfar", "asdf", "asdf", DateTime.Today, 120, "asdf", "asdf", "a3wr", 5.5M, null, "AEASDF");
		movie.AddThisItemToDatabase();
		MComment comment = new MComment(-1, 1, movie.ID, DateTime.Today, "afas", "rewrae");
		comment.UpdateM_Comment();
		int nT = MComment.GetM_Comment(movie.ID).Rows.Count;
		if(nT == 0)
			System.Console.WriteLine("获取或者插入出错");
		if (comment.IID != (int)MComment.GetM_Comment(movie.ID).Rows[0]["ID"])
			System.Console.WriteLine("ID出错");
		comment.DelM_Comment();
		nT = MComment.GetM_Comment(movie.ID).Rows.Count;
		if (nT != 0)
			System.Console.WriteLine("删除出错");
		comment = new MComment(-1, 1, movie.ID, DateTime.Today, "afas", "rewrae");
		comment.UpdateM_Comment();
		movie.DeleteItselfFromDatabase();
		nT = MComment.GetM_Comment(movie.ID).Rows.Count;
		if (nT != 0)
			System.Console.WriteLine("联系删除出错");
	}
}